package com.situ.object.dao.impl;



import com.situ.object.common.Page;
import com.situ.object.dao.StudentDao;
import com.situ.object.entity.Student;
import com.situ.object.utils.DBUtil;
import com.situ.object.vo.StudentVo;

import java.util.List;

public class StudentDaoImpl implements StudentDao {
    //合成
    private DBUtil dbUtil = new DBUtil();

    @Override
    public int add(Student student) {
        String sql = "INSERT INTO manage_system.student(studentName, studentPassword, studentSex, studentAge, teacherId, email, photo) VALUES (?,?,?,?,?,?,?)";
        return dbUtil.update(sql,student.getStudentName(),student.getStudentPassword(),student.getStudentSex(),student.getStudentAge(),student.getTeacherId(),student.getEmail(),student.getPhoto());
    }

    @Override
    public int delete(Integer id) {
        String sql = "update student set status = 1 where studentId = ?";      //软删除
//        String sql = "delete from student where id = ?";        //正常删除
        return dbUtil.update(sql,id);
    }

    @Override
    public int update(Student student) {
        String sql = "UPDATE manage_system.student SET studentName = ?, studentPassword = ?, studentSex = ?, studentAge = ?, teacherId = ?, status = ?, email = ?, photo = ? WHERE studentId = ? ";
        return dbUtil.update(sql,student.getStudentName(),student.getStudentPassword(),student.getStudentSex(),student.getStudentAge(),student.getTeacherId(),student.getStatus(),student.getEmail(),student.getPhoto(),student.getStudentId());
    }

    @Override
    public Student selectById(Student student) {
        String sql = "select * from student where studentId = ? ";
        return dbUtil.queryOne(Student.class,sql,student.getStudentId());
    }

    @Override
    public List<Student> selectAll() {
        String sql = "select * from student ";
        return dbUtil.query(Student.class,sql);
    }

    @Override
    public Long selectCount(String sql,List<Object> params) {
        String sqlCount = "select count(*) as count from ("+ sql +") as t" ;
        return dbUtil.queryOne(Page.class,sqlCount,params.toArray()).getCount();
    }

    @Override
    public List selectList(Integer page, Integer limit,String sql,List<Object> params) {
        int m = (page - 1) * limit;
        String sqlList = "select * from ("+ sql +") as t limit ?,?";
        params.add(m);
        params.add(limit);
        System.out.println("page"+page);
        System.out.println("limit"+limit);
        return dbUtil.query(StudentVo.class,sqlList,params.toArray());
    }
}
